package com.webex.ta.hydra.gui.table;

/**
 * Created by IntelliJ IDEA.
 * User: vegaz
 * Date: 2010-9-1
 * Time: 10:51:15
 * To change this template use File | Settings | File Templates.
 */

import java.awt.*;
import java.awt.event.MouseEvent;

import javax.swing.JComponent;
import javax.swing.event.MouseInputListener;
import javax.swing.plaf.basic.BasicTableUI;
import javax.swing.table.TableModel;

public class DragDropRowTableUI extends BasicTableUI {

    private boolean isDraggingRow = false;
    private int rowDragged;
    private int startDragPoint;
    private int dyOffset;

    protected MouseInputListener createMouseInputListener() {
        return new DragDropRowMouseInputHandler();
    }

    public void paint(Graphics g, JComponent c) {
        super.paint(g, c);

        if (isDraggingRow) {
            g.setColor(table.getParent().getBackground());
            Rectangle cellRect = table.getCellRect(rowDragged, 0, false);
            g.copyArea(cellRect.x, cellRect.y, table.getWidth(), table.getRowHeight(), cellRect.x, dyOffset);


            if (Math.abs(dyOffset) < table.getRowHeight()) {
                if (dyOffset < 0) {
                    g.fillRect(cellRect.x, cellRect.y + (table.getRowHeight() + dyOffset), table.getWidth(), (dyOffset * -1));
                } else {
                    g.fillRect(cellRect.x, cellRect.y, table.getWidth(), dyOffset);
                }
            } else {
                g.fillRect(cellRect.x, cellRect.y, table.getWidth(), table.getRowHeight());
//                g.setColor(Color.gray);
//                g.drawRect(cellRect.x, cellRect.y, table.getWidth(), table.getRowHeight());
            }
        }
    }

    class DragDropRowMouseInputHandler extends MouseInputHandler {

        public void mousePressed(MouseEvent e) {
            super.mousePressed(e);
            startDragPoint = (int) e.getPoint().getY();
            rowDragged = table.getSelectedRow();
        }

        public void mouseDragged(MouseEvent e) {
            int selectedRow = table.getSelectedRow();
            if (rowDragged >= 0) {
                isDraggingRow = true;
                int rowHeight = table.getRowHeight();
                int middleOfSelectedRow = (rowHeight * selectedRow) + (rowHeight / 2);

                int toRow = -1;
                int yMousePoint = (int) e.getPoint().getY();

                if (yMousePoint < (middleOfSelectedRow - rowHeight)) {
                    // Move row up
                    toRow = selectedRow - 1;
                } else if (yMousePoint > (middleOfSelectedRow + rowHeight)) {
                    // Move row down
                    toRow = selectedRow + 1;
                }

                if (toRow >= 0 && toRow < table.getRowCount()) {
                    TableModel model = table.getModel();

//                    for (int i = 0; i < model.getColumnCount(); i++) {
//                        Object fromValue = model.getValueAt(fromRow, i);
//                        Object toValue = model.getValueAt(toRow, i);
//
//                        model.setValueAt(toValue, fromRow, i);
//                        model.setValueAt(fromValue, toRow, i);
//                    }
                    table.setRowSelectionInterval(toRow, toRow);
//                    startDragPoint = yMousePoint;
                }
                dyOffset = (startDragPoint - yMousePoint) * -1;
                table.repaint();
            }
        }

        public void mouseReleased(MouseEvent e) {
            super.mouseReleased(e);
            isDraggingRow = false;
            table.repaint();
        }
    }
}

